package org.erepublik.company.scanner;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.UnmarshalException;

import org.erepublik.company.ECountry;
import org.erepublik.company.EProductType;
import org.erepublik.company.Job;
import org.erepublik.company.generated.CompanyT;
import org.erepublik.company.generated.JobOfferT;
import org.erepublik.company.service.DatabaseProvider;
import org.erepublik.company.service.IExchangeRateService;
import org.erepublik.company.service.IVatService;
import org.erepublik.company.utils.WebpageReader;
import org.erepublik.company.xml.XMLParsersers;

public class JobsScanner {

    private IVatService vatService;
    private IExchangeRateService currencyService;
    private DatabaseProvider database = new DatabaseProvider();
    
    public JobsScanner(IVatService vatService, IExchangeRateService currencyService) {
        this.vatService = vatService;
        this.currencyService = currencyService;
    }
    
    public Job[] getJobsFromInternet() throws Exception {
        long[] ids = database.getAvticeCompaniesId();
        return getJobsFromInternet(ids);
    }
    
    public Job[] getJobsFromInternet(long[] ids) throws Exception  {
        ArrayList<Job> jobs = new ArrayList<Job>();
        for (int i=0; i < ids.length; i++) {
            Job[] jobArray = getJobsFromInternet( ids[i] );
            System.out.println(i+"skanuje joby z firmy "+ids[i]);
            for (Job job : jobArray) {
                jobs.add(job);
            }
        }
        return jobs.toArray(new Job[0]);
    }
    
    private Job[] getJobsFromInternet(long id) throws Exception {
        
        CompanyT company = null;
        try {
            company = XMLParsersers.parseCompany( WebpageReader.getStreamToIrl("http://api.erepublik.com/v2/feeds/companies/"+id) );
        } catch (UnmarshalException e) {
            System.out.println("nie udalo sie zeskanowac firmy "+id +" prawdopodobnie zostala usunieta");
            return new Job[0];
        }
        return getJobs(company);
    }
    
    
    private Job[] getJobs(CompanyT company) throws Exception {
        
        ArrayList<Job> jobsList = new ArrayList<Job>();
        List<JobOfferT> jobs =  company.getJobOffers().getJobOffer();
        for (JobOfferT jobOfferT : jobs) {
            Job job = new Job();
//            ESkill skill = ESkill.getSkillById( jobOfferT.getSkillId() );
//            job.setSkill(skill);
            job.setCompanyId(company.getId());
            ECountry country =  ECountry.getCountryByName( company.getCountry().getName() );
            job.setCountry( country );
            job.setMinimalSkillByXP( jobOfferT.getRequiredSkill() );

            job.setSalaryInLocalBeforeTax( jobOfferT.getSalary() );
            job.setOfferNumbers( jobOfferT.getNumberOfJobs() );
            double tax = vatService.getVat().get( country ).getIncomeTax(EProductType.getProductTypeByName( company.getIndustry().getName() ) );
            
            job.setSalaryInGoldBefore( job.getSalaryInLocalBeforeTax() *  currencyService.getExchangeRates().get(country) );
            job.setSalaryInGoldAfterTax( job.getSalaryInGoldBefore() * (1-tax)  );
            job.setIncomeTax( tax );
            job.setCompanyName(company.getName());
            job.setProductType( EProductType.getProductTypeByName(  company.getIndustry().getName() ) );
            job.setQuality( company.getCustomizationLevel() );
            jobsList.add(job);
        }
        
        return jobsList.toArray( new Job[0] );
    }
    
    
}
